<template>
  <view class="work-container">
    <!-- 轮播图 -->
    <uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
      <swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
        <swiper-item v-for="(item, index) in data" :key="index">
          <view class="swiper-item" @click="clickBannerItem(item)">
            <image :src="item.image" mode="aspectFill" :draggable="false" />
          </view>
        </swiper-item>
      </swiper>
    </uni-swiper-dot>

    <!-- 功能模块 -->
    <view class="module-section">
      <view class="section-title">功能模块</view>
      <view class="menu-grid">
        <view 
          v-for="(item, index) in visibleMenuList" 
          :key="index"
          class="menu-item"
          @click="onMenuClick(item)"
        >
          <view class="menu-icon" :style="{ backgroundColor: item.bgColor }">
            <image :src="item.iconImage" class="icon-image" mode="aspectFit" />
          </view>
          <text class="menu-text">{{ item.name }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import { mapGetters } from 'vuex'
  
  export default {
    data() {
      return {
        current: 0,
        swiperDotIndex: 0,
        data: [{
            image: '/static/images/banner/banner01.jpg'
          },
          {
            image: '/static/images/banner/banner02.jpg'
          },
          {
            image: '/static/images/banner/banner03.jpg'
          }
        ],
        // 所有菜单项，包含权限配置
        menuList: [
          {
            name: '开始巡班',
            icon: 'icon-form',
            iconImage: '/static/images/icon/icon_agry.png',
            bgColor: '#3c96f3',
            url: '/pages/patrol/index',
            roles: ['patrol_teacher'] // 只有巡班老师可以看到
          },
          {
            name: '教师上课',
            icon: 'icon-teacher',
            iconImage: '/static/images/icon/teacher.png', // 临时使用 patrol 图标，请替换为 teacher.png
            bgColor: '#3c96f3',
            url: '/pages/patrol/teacher-status',
            roles: ['patrol_teacher'] // 只有巡班老师可以看到
          },
          {
            name: '课程安排',
            icon: 'icon-schedule',
            iconImage: '/static/images/icon/icon_agry.png',
            bgColor: '#67c23a',
            url: '/pages/work/schedule/index',
            roles: ['patrol_teacher', 'normal_teacher'] // 所有老师都可以看到
          },
          {
            name: '违纪查看',
            icon: 'icon-search',
            iconImage: '/static/images/icon/Violation.png',
            bgColor: '#3c96f3',
            url: '/pages/patrol/violation-list',
            roles: ['patrol_teacher', 'normal_teacher'] // 巡班老师和普通老师都可以看到
          },
          {
            name: '违纪排名',
            icon: 'icon-ranking',
            iconImage: '/static/images/icon/ptp-tj-paihangbang.png',
           bgColor: '#3c96f3',
            url: '/pages/patrol/ranking',
            roles: ['patrol_teacher', 'normal_teacher'] // 巡班老师和普通老师都可以看到
          }
        ]
      }
    },
    computed: {
      ...mapGetters(['roles']),
      // 根据用户角色过滤可见的菜单项
      visibleMenuList() {
        if (!this.roles || this.roles.length === 0) {
          // 如果没有角色信息，默认只显示违纪查看和违纪排名
          return this.menuList.filter(item => item.name !== '巡班管理')
        }
        
        // 检查用户是否是巡班老师
        const isPatrolTeacher = this.roles.some(role => 
          role === 'patrol_teacher' || 
          role.toLowerCase().includes('patrol') ||
          role.toLowerCase().includes('巡班')
        )
        
        // 如果是巡班老师，显示所有菜单
        if (isPatrolTeacher) {
          return this.menuList
        }
        
        // 普通老师只显示违纪查看和违纪排名
        return this.menuList.filter(item => item.roles.includes('normal_teacher'))
      }
    },
    onShow() {
      // 页面显示时刷新角色信息
      this.checkUserRole()
    },
    methods: {
      checkUserRole() {
        // console.log('当前用户角色:', this.roles)
      },
      clickBannerItem(item) {
        console.info(item)
      },
      changeSwiper(e) {
        this.current = e.detail.current
      },
      onMenuClick(item) {
        if (item.url) {
          uni.navigateTo({
            url: item.url,
            fail: (err) => {
              console.error('跳转失败:', err)
              this.$modal.msgError('页面跳转失败')
            }
          })
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  /* #ifndef APP-NVUE */
  page {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    background-color: #f5f5f5;
    min-height: 100%;
    height: auto;
  }

  view {
    font-size: 14px;
    line-height: inherit;
  }
  /* #endif */

  .work-container {
    background-color: #f5f5f5;
  }

  .swiper-box {
    height: 150px;
  }

  .swiper-item {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #fff;
    height: 300rpx;
    line-height: 300rpx;
  }

  .module-section {
    margin: 20px 15px;
    background-color: #fff;
    border-radius: 12px;
    padding: 20px 15px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  }

  .section-title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-bottom: 20px;
    padding-left: 10px;
    border-left: 4px solid #5DADE2;
  }

  .menu-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
    padding: 10px 0;
  }

  .menu-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px 10px;
    background-color: #fafafa;
    border-radius: 12px;
    transition: all 0.3s;
    cursor: pointer;
  }

  .menu-item:active {
    transform: scale(0.95);
    background-color: #f0f0f0;
  }

  .menu-icon {
    width: 50px;
    height: 50px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 10px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  }

  .menu-icon .iconfont {
    font-size: 28px;
    color: #fff;
  }
  
  .icon-image {
    width: 30px;
    height: 30px;
  }

  .menu-text {
    font-size: 13px;
    color: #333;
    text-align: center;
    font-weight: 500;
  }

  @media screen and (min-width: 500px) {
    .uni-swiper-dot-box {
      width: 400px;
      /* #ifndef APP-NVUE */
      margin: 0 auto;
      /* #endif */
      margin-top: 8px;
    }

    .image {
      width: 100%;
    }
  }
</style>
